package com.wordlearning.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wordlearning.app.entity.Word;
import com.wordlearning.app.repository.WordRepository;
import com.wordlearning.app.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class WordServiceImpl implements WordService {

    private final WordRepository wordRepository;

    @Autowired
    public WordServiceImpl(WordRepository wordRepository) {
        this.wordRepository = wordRepository;
    }

    @Override
    @Transactional
    public Word createWord(Word word) {
        if (wordRepository.existsByContent(word.getContent())) {
            throw new IllegalArgumentException("Word already exists: " + word.getContent());
        }
        wordRepository.insert(word);
        return word;
    }

    @Override
    public List<Word> getAllWords(int page, int pageSize) {
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        // 使用searchWords方法实现分页查询，传入空关键字查询所有记录
        return wordRepository.searchWords("", pageSize, offset);
    }

    @Override
    public Optional<Word> getWordById(Long id) {
        return wordRepository.selectById(id);
    }

    @Override
    public Optional<Word> getWordByContent(String content) {
        return wordRepository.findByContent(content);
    }

    @Override
    @Transactional
    public Word updateWord(Long id, Word word) {
        Word existingWord = wordRepository.selectById(id)
                .orElseThrow(() -> new IllegalArgumentException("Word not found with id: " + id));

        existingWord.setContent(word.getContent());
        existingWord.setTranslation(word.getTranslation());
        existingWord.setPronunciation(word.getPronunciation());
        existingWord.setPartOfSpeech(word.getPartOfSpeech());
        existingWord.setExampleSentence(word.getExampleSentence());
        existingWord.setDefinition(word.getDefinition());
        existingWord.setLanguage(word.getLanguage());

        wordRepository.updateById(existingWord);
        return existingWord;
    }

    @Override
    @Transactional
    public void deleteWord(Long id) {
        if (wordRepository.selectById(id) == null) {
            throw new IllegalArgumentException("Word not found with id: " + id);
        }
        wordRepository.deleteById(id);
    }

    @Override
    public boolean existsByContent(String content) {
        return wordRepository.existsByContent(content);
    }

    @Override
    public List<Word> searchWords(String keyword) {
        QueryWrapper<Word> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("content", keyword)
                .or().like("translation", keyword)
                .or().like("definition", keyword);
        return wordRepository.selectList(queryWrapper);
    }
}